//
//  ViewController.m
//  searchController
//
//  Created by MB__Lee on 2018/4/13.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

#import "SearchVC.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate>
@property(nonatomic,strong)SearchVC *vc;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *mut;


@end

@implementation ViewController
-(NSMutableArray *)mut{
    if (!_mut) {
        _mut  = [NSMutableArray array];
        
        
        for (int i = 0;i< 100 ; i++) {
            NSString *name = [NSString stringWithFormat:@"%zd%@",i,[self shuffledAlphabet]];
            [_mut addObject:name];
            
        }
    }
    return  _mut;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    self.title = @"搜索列表";
    
    
    
    _vc  = [[SearchVC alloc] init];
    _vc.AMut = self.mut;
    
    
    UISearchController *search = [[UISearchController alloc] initWithSearchResultsController:_vc];
    search.searchResultsUpdater = _vc;
    search.delegate = self;
    
    [search.searchBar sizeToFit];
    search.searchBar.delegate = self;
    search.searchBar.placeholder = @"搜索";
    
    search.dimsBackgroundDuringPresentation = NO;
    search.obscuresBackgroundDuringPresentation = YES;

    
    
    search.searchBar.showsCancelButton = NO;
    search.searchBar.tintColor  = [UIColor blueColor];
    
    
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.searchController = search;
    }else{
        _tableview.tableHeaderView = search.searchBar;
    }
  

    //这句话很重要，必须要加，不然后面push进来的视图 都只能显示一部分；
    self.definesPresentationContext = YES;
    
    _vc.searchBar = search.searchBar;
    _vc.nav = self.navigationController;
    
    
}
//产生3个随机字母
- (NSString *)shuffledAlphabet {
    NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
    
    NSString *strTest = [[NSString alloc]init];
    for (int i=0; i<3; i++) {
        int x = arc4random() % 25;
        strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
    }
    return strTest;
}

#pragma mark --tableivewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.mut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identi = @"cell";
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:identi];
    
    if (!cell) {
        cell  =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identi];
    }
    
    cell.textLabel.text  = self.mut[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *vc = [UIViewController new];
    vc.view.backgroundColor = [UIColor greenColor];
    [self.navigationController pushViewController: vc animated:YES];
}

#pragma mark --UISearchControllerDelegate

-(void)willPresentSearchController:(UISearchController *)searchController{
    NSLog(@"======%s",__func__);
}
-(void)didPresentSearchController:(UISearchController *)searchController{
    NSLog(@"======%s",__func__);
}
-(void)willDismissSearchController:(UISearchController *)searchController{
    NSLog(@"======%s",__func__);
}
-(void)didDismissSearchController:(UISearchController *)searchController{
    NSLog(@"======%s",__func__);
}
-(void)presentSearchController:(UISearchController *)searchController{
    NSLog(@"======%s",__func__);
}
#pragma mark --searchResult
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSLog(@"----");
}
#pragma mark --searchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    //将cancle 修改为取消
    [searchBar setShowsCancelButton:YES animated:YES];
    for (id obj in [searchBar subviews]) {
        if ([obj isKindOfClass:[UIView class]]) {
            for (id obj2 in [obj subviews]) {
                if ([obj2 isKindOfClass:[UIButton class]]) {
                    UIButton *btn = (UIButton *)obj2;
                    [btn setTitle:@"取消" forState:UIControlStateNormal];
                }
            }
        }
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
